package wyp.library.ui.vc;

import java.util.ArrayList;

import wyp.library.tools.WypSizeTool;
import wyp.library.ui.view.WypGroupView;
import wyp.library.ui.view.WypGroupView.WypGroupViewDelegate;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class WypGridViewController {

	public static final String TAG = WypGridViewController.class.getSimpleName();

	public LinearLayout mainLayout;
	private ScrollView scrollView;
	private ArrayList<WypGroupView> mGridViewList = new ArrayList<WypGroupView>();

	// private LinearLayout.LayoutParams mGridParams = new
	// LinearLayout.LayoutParams(0, -1, 1);
	// public int columnNum = 3;
	public int verticalSplitLineColor = Color.GRAY;
	public int horizontalSplitLineColor = Color.GRAY;
	public int verticalSplitLineWidth_px = 2;
	public int horizontalSplitLineWidth_px = 2;

	public WypGridViewController(ScrollView _scrollView) {
		super();
		this.scrollView = _scrollView;
	}

	public void addGridView(WypGroupView _groupView) {

		mGridViewList.add(_groupView);
	}

	public void show(int _rowHeight_px, final WypGroupViewDelegate _delegate, int... _weightArray) {
		if (_weightArray == null) {
			_weightArray = new int[] { 1, 1 };
		}
		int _linearNum = mGridViewList.size() / _weightArray.length;
		if (mGridViewList.size() % _weightArray.length > 0) {
			_linearNum += 1;
		}

		if (mainLayout == null) {
			mainLayout = this._createMainLayout();
		}
		LinearLayout.LayoutParams _rowLayoutParams = new LinearLayout.LayoutParams(-1, _rowHeight_px);

		for (int i = 0; i < _linearNum; i++) {
			LinearLayout _rowLayout = _createRowLayout(_rowLayoutParams);

			for (int j = 0; j < _weightArray.length; j++) {
				int _index = i * _weightArray.length + j;
				LinearLayout.LayoutParams mGridParams = new LinearLayout.LayoutParams(0, -1, _weightArray[j]);
				try {
					WypGroupView _gridView = mGridViewList.get(_index);
					_gridView.setLayoutParams(mGridParams);
					_gridView.addView(_createBottomLine());
					if (j + 1 != _weightArray.length) {
						_gridView.addView(_createRightLine());
					}
					if (_gridView.getDelegate() == null) {
						_gridView.setDelegate(_delegate);
					}

					_rowLayout.addView(_gridView);
				} catch (IndexOutOfBoundsException e) {
					View _nullView = new View(this.scrollView.getContext());
					_nullView.setLayoutParams(mGridParams);
					_nullView.setBackgroundColor(Color.TRANSPARENT);
					_rowLayout.addView(_nullView);
				}

			}
			mainLayout.addView(_rowLayout);
		}
		this.scrollView.addView(mainLayout);
	}

	private LinearLayout _createRowLayout(LinearLayout.LayoutParams _params) {
		LinearLayout _linearLayout = new LinearLayout(this.scrollView.getContext());
		_linearLayout.setLayoutParams(_params);
		_linearLayout.setOrientation(LinearLayout.HORIZONTAL);
		_linearLayout.setBackgroundColor(Color.TRANSPARENT);
		_linearLayout.setGravity(Gravity.CENTER);
		return _linearLayout;
	}

	private LinearLayout _createMainLayout() {
		LinearLayout _linearLayout = new LinearLayout(this.scrollView.getContext());
		_linearLayout.setLayoutParams(new LinearLayout.LayoutParams(WypSizeTool.MATCH_PARENT, WypSizeTool.WRAP_CONTENT));
		_linearLayout.setOrientation(LinearLayout.VERTICAL);
		_linearLayout.setBackgroundColor(Color.TRANSPARENT);
		return _linearLayout;
	}

	private View _createBottomLine() {
		WypGroupView.LayoutParams _bottomLineParams = new WypGroupView.LayoutParams(-1, horizontalSplitLineWidth_px);
		_bottomLineParams.addRule(WypGroupView.ALIGN_PARENT_BOTTOM, WypGroupView.TRUE);
		View _bottomLine = new View(this.scrollView.getContext());
		_bottomLine.setLayoutParams(_bottomLineParams);
		_bottomLine.setBackgroundColor(this.horizontalSplitLineColor);
		return _bottomLine;
	}

	private View _createRightLine() {
		WypGroupView.LayoutParams _rightLineParams = new WypGroupView.LayoutParams(verticalSplitLineWidth_px, -1);
		_rightLineParams.addRule(WypGroupView.ALIGN_PARENT_RIGHT, WypGroupView.TRUE);
		View _rightLine = new View(this.scrollView.getContext());
		_rightLine.setLayoutParams(_rightLineParams);
		_rightLine.setBackgroundColor(this.verticalSplitLineColor);
		return _rightLine;
	}

}
